#include "fig_enemynormal_backattacker.h"
#include "fig_player_craft.h"
#include "fig_enemybullet_master.h"

#include "num/rand.h"
#include "math/facing.h"

#include "util/mmgr.h"		    // Needs to come in early
#include "util/common_macros.h"		// macros

const float		FIGEnemyNormalBackAttacker::MIN_DISTANCE = 0.15f;
const float		FIGEnemyNormalBackAttacker::MAX_DISTANCE = 0.3f;


FIGEnemyNormalBackAttacker::FIGEnemyNormalBackAttacker(FIGEnemyCraft::ENEMY_TYPES EnemyType) :
													FIGEnemy3DRotate(EnemyType),
													m_IsGoingDown(false)
{
	
	
	 m_BeforeGoDownCounter = 1.5f;   
	 m_bStartSlowDown = false;
	 m_Speed = 0.3f;
	 m_Accel = 2.0f;
	 m_RotationSpeed = 360.0f;
	 m_IsHitTestCircle = true;
	 m_ShootCounter.SetLimit( num::RAND32::Instance().RandFloat(3.0f,4.5f));

	 
}




void	FIGEnemyNormalBackAttacker::Tick(float dt)
{
	if(!m_IsAlive)
		return;
    
	FIGEnemy3DRotate::Tick(dt);

	if(m_DelaySpawn > 0)
		return;
	
	math::Vec2 TargetPos = math::Facing::VectorFromAngle((float)m_DegreeFromPlayer);
	TargetPos = FIGPlayerCraft::Instance().GetPos() + (TargetPos  * m_PreferrDistance);
	math::Vec2 Dir = TargetPos - m_Pos;
	math::Vec2 Step;


	if( Dir.Magnitude() <= m_PreferrDistance)		
	{

		if( m_DegreeFromPlayer > m_DegreeFromPlayer2)
			m_DegreeFromPlayer -= ( 50.0f * dt);
		if( m_DegreeFromPlayer < m_DegreeFromPlayer2)
			m_DegreeFromPlayer += ( 50.0f * dt);

		if(m_DegreeFromPlayer <	 m_DegreeFromPlayer2 + 1 && m_DegreeFromPlayer > m_DegreeFromPlayer2 - 1)
			m_IsGoingDown = true;

	}
	           	

	Dir.Normalize();
	Step =  Dir * dt * m_Speed;
	Step *= m_Accel;
	if(m_IsGoingDown)
	{
		Step.x *= 0.5f;
		Step.y *= 0.7f;
		
	}
	if(Step.y < 0)
		Step.y *= -1;
	m_Pos += Step;

	if(m_Accel > 1.0f)
		m_Accel -= dt;
	
	if(m_IsGoingDown)
	{
		m_PreferrDistance += dt;
	}

		//if(m_ShootCount == 0)
		
	
	AimToPlayer(dt);


}

void	FIGEnemyNormalBackAttacker::ReSpawn()
{
	
	if(num::RAND32::Instance().RandInt(0,1) == 0)
	{
		// LEFT SIDE
		m_DegreeFromPlayer = (float) num::RAND32::Instance().RandInt(280 , 350);	
		m_DegreeFromPlayer2 = (float) num::RAND32::Instance().RandInt(190 , 260);
		
	}
	else
	{

		m_DegreeFromPlayer = (float) num::RAND32::Instance().RandInt(10 , 80 );	
		m_DegreeFromPlayer2	= (float) num::RAND32::Instance().RandInt(90 , 170 );
			
		
	}

	FIGEnemyCraft::ReSpawn();
	float X = num::RAND32::Instance().RandFloat(0.0f,1.0f);
	m_Pos = math::Vec2(X,-0.1f);

	m_PreferrDistance =	 num::RAND32::Instance().RandFloat(MIN_DISTANCE, MAX_DISTANCE);
	m_IsReadyToFire = true;	
}


void		FIGEnemyNormalBackAttacker::OnShoot()
{
	if(FIGCraft::IsInSideScreen())
        if(num::RAND32::Instance().RandInt(0,1) == 0)
        {
			FIGEnemyBulletMaster::Instance().Shoot(
												this,
												math::Vec2(0,0),
												FIGEnemyBulletMaster::BULLET_TYPE_CIRCLE_DOUBLELAYER,
												FIGBullet::SHOOT_STYLES_SINGLE,
												m_Rotation + 180.0f,
												0,
												false,
												NULL
												);
        }
}



